//
//  WKNewViewController.m
//  WKDemo
//
//  Created by wangzhaohui-Mac on 14-8-2.
//  Copyright (c) 2014年 com.app. All rights reserved.
//

#import "WKOneNewViewController.h"
#import "WKHomeHttpTool.h"
#import "WKOneViewCell.h"
#import "WKStatusFrame.h"
#import "WKDetailViewController.h"

@interface WKOneNewViewController ()

/**frame模型数组*/
@property (nonatomic,strong) NSMutableArray *statusFrames;
@end

@implementation WKOneNewViewController

- (NSArray *)statusFrames
{
    if (_statusFrames == nil) {
        _statusFrames = [NSMutableArray array];
    }
    return _statusFrames;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    

//    //添加下拉刷新控件
//    [self.tableView addHeaderWithTarget:self action:@selector(loadNewdata)];
//    //添加上拉刷新控件
//    [self.tableView addFooterWithTarget:self action:@selector(loadMoreData)];
    
    //监听刷新按钮点击的通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refreshData) name:WKDidDeletedNotificationTwo object:nil];
    
}
/**
  *  上拉刷新
  */
- (void)loadMoreData
{
    WKHomeParam *param = [[WKHomeParam alloc] init];
    
    WKStatusFrame *lastStatusFrame = self.statusFrames.lastObject;
    WKResult *lastStatus = lastStatusFrame.result;
    
    if (lastStatus) {
        // max_id	false	int64	若指定此参数，则返回ID小于或等于max_id的微博，默认为0
        param.maxid = [NSString stringWithFormat:@"%lld",[lastStatus.id longLongValue]+1];
    }
    
    [WKHomeHttpTool getNewestMoreDataListWithParam:param success:^(WKHomeResult *result) {
        //取出微博模型数组
        NSArray *statusesDict = [self statusFrameWithStatuses:result.homeDatas];
        
        //将新数据插入到旧数据的后面
        [self.statusFrames addObjectsFromArray:statusesDict];
        
        //刷新数据
        [self.tableView reloadData];
        
        //停止下拉刷新
        [self.tableView footerEndRefreshing];
    } failure:^(NSError *error) {
        //停止下拉刷新
        [self.tableView footerEndRefreshing];
    }];
}

/**
 *  点击导航栏右侧按钮下拉刷新
 */
- (void)refreshData
{
    [self.tableView headerBeginRefreshing];
//    WKLog(@"WKDidDeletedNotificationTwo");
}

- (NSMutableArray *)statusFrameWithStatuses:(NSArray *)statuses
{
    NSMutableArray *frames = [NSMutableArray array];
        
    for (WKResult *result in statuses) {
        WKStatusFrame *frame = [[WKStatusFrame alloc] init];
        //传递微博模型数据，计算所有子控件的frame
        frame.result = result;
        [frames addObject:frame];
    }
    return frames;
}

- (void)dealloc
{
    //移除通知
    [[NSNotificationCenter defaultCenter] removeObserver:self];

}
/**
 *  加载新数据
 */
- (void)loadNewdata
{
    WKHomeParam *param = [[WKHomeParam alloc] init];
    [WKHomeHttpTool getNewestNewDataListWithParam:param success:^(WKHomeResult *result) {
        //如果返回数据为空则返回;
        if (result == nil ) return;
        self.statusFrames = [self statusFrameWithStatuses:result.homeDatas];
        //显示底部刷新控件
        self.tableView.footerHidden = NO;
        //刷新数据
        [self.tableView reloadData];
        
        //停止下拉刷新
        [self.tableView headerEndRefreshing];
    } failure:^(NSError *error) {
        WKLog(@"%@",error);
    }];
  
    
}
#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.statusFrames.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    WKOneViewCell *cell = [WKOneViewCell cellWithTableView:tableView];
    
    WKStatusFrame *statusFrame = self.statusFrames[indexPath.row];
    cell.statusFrame = statusFrame;
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //动画取消cell选中
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    WKStatusFrame *statusFrame = self.statusFrames[indexPath.row];
    
    WKDetailViewController *dtVc = [[WKDetailViewController alloc] init];
    dtVc.statusFrame = statusFrame;
    [self.naVc pushViewController:dtVc animated:YES];
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    WKStatusFrame *statusFrame = self.statusFrames[indexPath.row];
    return statusFrame.cellHeigth;
}

@end
